

#include "stdafx.h"
#include "pf_variabledict.h"
#include "pf_debug.h"

/**
 * Constructor.
 */
PF_VariableDict::PF_VariableDict() {
}


/**
 * Removes all variables in the blocklist.
 */
void PF_VariableDict::clear() {
    variables.clear();
}


/**
 * Activates the given block.
 * Listeners are notified.
 */
//void PF_VariableDict::activateBlock(const QString& name) {
//	activateBlock(findBlock(name));
//}

/**
 * Activates the given block.
 * Listeners are notified.
 */
/*void PF_VariableDict::activateBlock(PF_Block* block) {
	activeBlock = block;
	
	for (uint i=0; i<blockListListeners.count(); ++i) {
		PF_VariableDictListener* l = blockListListeners.at(i);
 
		l->blockActivated(activeBlock);
	}
}*/




/**
 * Adds a variable to the variable dictionary. If a variable with the 
 * same name already exists, is will be overwritten.
 */
void PF_VariableDict::add(const QString& key,
                                  const QString& value, int code) {
    PF_DEBUG->print("PF_VariableDict::addVariable()");

    if (key.isEmpty()) {
        PF_DEBUG->print("PF_VariableDict::addVariable(): "
                        "No empty keys allowed.",
                        PF_Debug::D_WARNING);
        return;
    }

    variables.insert(key, PF_Variable(value, code));
}



/**
 * Adds a variable to the variable dictionary. If a variable with the 
 * same name already exists, is will be overwritten.
 */
void PF_VariableDict::add(const QString& key, int value, int code) {
    PF_DEBUG->print("PF_VariableDict::addVariable()");

    if (key.isEmpty()) {
        PF_DEBUG->print("PF_VariableDict::addVariable(): "
                        "No empty keys allowed.",
                        PF_Debug::D_WARNING);
        return;
    }

    variables.insert(key, PF_Variable(value, code));
}



/**
 * Adds a variable to the variable dictionary. If a variable with the 
 * same name already exists, is will be overwritten.
 */
void PF_VariableDict::add(const QString& key, double value, int code) {
    PF_DEBUG->print("PF_VariableDict::addVariable()");

    if (key.isEmpty()) {
        PF_DEBUG->print("PF_VariableDict::addVariable(): "
                        "No empty keys allowed.",
                        PF_Debug::D_WARNING);
        return;
    }

    variables.insert(key, PF_Variable(value, code));
}



/**
 * Adds a variable to the variable dictionary. If a variable with the 
 * same name already exists, is will be overwritten.
 */
void PF_VariableDict::add(const QString& key,
                                  const PF_Vector& value, int code) {
    PF_DEBUG->print("PF_VariableDict::addVariable()");

    if (key.isEmpty()) {
        PF_DEBUG->print("PF_VariableDict::addVariable(): "
                        "No empty keys allowed.",
                        PF_Debug::D_WARNING);
        return;
    }

    variables.insert(key, PF_Variable(value, code));
}



/**
 * Gets the value for the given variable.
 *
 * @param key Key of the variable.
 * @param def Default value.
 *
 * @return The value for the given variable or the given default value
 * if the variable couldn't be found.
 */
PF_Vector PF_VariableDict::getVector(const QString& key,
        const PF_Vector& def) {

    PF_Vector ret;
    QHash<QString, PF_Variable>::iterator i = variables.find(key);
     if (i != variables.end() || i.value().getType()==LX_Define::VariableVector) {
        ret = i.value().getVector();
    } else {
        ret = def;
    }
    return ret;
}



/**
 * Gets the value for the given variable.
 *
 * @param key Key of the variable.
 * @param def Default value.
 *
 * @return The value for the given variable or the given default value
 * if the variable couldn't be found.
 */
QString PF_VariableDict::getString(const QString& key,
        const QString& def) {

    QString ret;

	PF_DEBUG->print("PF_VariableDict::getString: 001");
        PF_DEBUG->print("PF_VariableDict::getString: key: '%s'", key.toLatin1().data());
	
    QHash<QString, PF_Variable>::iterator i = variables.find(key);
        PF_DEBUG->print("PF_VariableDict::getString: 002");

    if (i == variables.end()) {
		PF_DEBUG->print("PF_VariableDict::getString: 003");
        ret = def;
        } else if (i.value().getType() != LX_Define::VariableString) {
		PF_DEBUG->print("PF_VariableDict::getString: 004");
		ret = def;
    } else {
		PF_DEBUG->print("PF_VariableDict::getString: 005");
        ret = i.value().getString();
    }
	PF_DEBUG->print("PF_VariableDict::getString: 006");

    return ret;
}



/**
 * Gets the value as int for the given variable.
 *
 * @param key Key of the variable.
 * @param def Default value.
 *
 * @return The value for the given variable or the given default value
 * if the variable couldn't be found.
 */
int PF_VariableDict::getInt(const QString& key,
                                    int def) {

    int ret;
    QHash<QString, PF_Variable>::iterator i = variables.find(key);
     if (i != variables.end() || i.value().getType()==LX_Define::VariableInt) {
        ret = i.value().getInt();
    } else {
        ret = def;
    }
    return ret;
}



/**
 * Gets the value as double for the given variable.
 *
 * @param key Key of the variable.
 * @param def Default value.
 *
 * @return The value for the given variable or the given default value
 * if the variable couldn't be found.
 */
double PF_VariableDict::getDouble(const QString& key,
        double def) {

    double ret;
    QHash<QString, PF_Variable>::iterator i = variables.find(key);
     if (i != variables.end() || i.value().getType()==LX_Define::VariableDouble) {
        ret = i.value().getDouble();
    } else {
        ret = def;
    }
    return ret;
}

/**
 * Notifies the listeners about layers that were added. This can be
 * used after adding a lot of variables without auto-update.
 */
/*
void PF_VariableDict::addBlockNotification() {
    for (uint i=0; i<blockListListeners.count(); ++i) {
        PF_VariableDictListener* l = blockListListeners.at(i);
        l->blockAdded(NULL);
    }
}
*/

/**
 * Removes a variable from the list.
 * TODO: Listeners are notified after the block was removed from 
 * the list but before it gets deleted.
 */
void PF_VariableDict::remove(const QString& key) {
    PF_DEBUG->print("PF_VariableDict::removeVariable()");

    // here the block is removed from the list but not deleted
    variables.remove(key);

}



/**
 * Dumps the variables to stdout.
 */
std::ostream& operator << (std::ostream& os, PF_VariableDict& d) {

    os << "Variables: \n";
    QHash<QString, PF_Variable>::iterator it = d.variables.begin();
    while (it != d.variables.end()) {
        os << it.key().toLatin1().data() << ": ";
        switch (it.value().getType()) {
        case LX_Define::VariableVoid:
            os << "void\n";
            break;
        case LX_Define::VariableInt:
            os << "int " << it.value().getInt() << "\n";
            break;
        case LX_Define::VariableDouble:
            os << "double " << it.value().getDouble() << "\n";
            break;
        case LX_Define::VariableVector:
            os << "vector " << it.value().getVector() << "\n";
            break;
        case LX_Define::VariableString:
            os << "string " << it.value().getString().toLatin1().data() << "\n";
            break;
        }
        ++it;
    }
    os << std::endl;

    return os;
}

